package com.example.livereplay.common.controller;

import com.example.livereplay.common.model.enity.SysAttachment;
import com.example.livereplay.common.model.request.GetAttachmentRequest;
import com.example.livereplay.common.model.request.InsertAttachmentRequest;
import com.example.livereplay.common.model.request.InsertTournamentRequest;
import com.example.livereplay.common.service.FileUploadService;
import com.example.livereplay.common.service.SysAttachmentService;
import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.common.web.ResultBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sysAttachment")
@Tag(name = "评论管理")
public class SysAttachmentController {
    @Resource
    private SysAttachmentService sysAttachmentService;
    @Lazy
    @Resource
    private FileUploadService fileUploadService;

    @PostMapping("/deleteSingleFile")
    @Operation(summary = "删除单一文件")
    public ResultBean<Boolean> deleteSingleFile(@RequestBody @Valid String id) {
        return ResultBean.restResult(fileUploadService.deleteSingleFile(id), ErrorCodeInfo.OK);
    }

    @PostMapping("/deleteFiles")
    @Operation(summary = "删除多个文件")
    public ResultBean<Boolean> deleteFiles(@RequestBody @Valid List<String> fileIds) {
        return ResultBean.restResult(fileUploadService.deleteBatchFile(fileIds), ErrorCodeInfo.OK);
    }

    @PostMapping("/getAttachmentList")
    @Operation(summary = "管理员查找附件")
    public ResultBean<List<SysAttachment>> getAttachmentList(@RequestBody @Valid GetAttachmentRequest getAttachmentRequest) {
        return ResultBean.restResult(sysAttachmentService.getAttachmentList(getAttachmentRequest), ErrorCodeInfo.OK);
    }

    @PostMapping("/getAttachmentByVideo")
    @Operation(summary = "对应id查找赛事")
    public ResultBean<List<SysAttachment>> getAttachmentByVideo(@RequestBody @Valid GetAttachmentRequest getAttachmentRequest) {
        return ResultBean.restResult(sysAttachmentService.getAttachmentByVideo(getAttachmentRequest), ErrorCodeInfo.OK);
    }

    @PostMapping("/getAttachmentByName")
    @Operation(summary = "关键字搜索")
    public ResultBean<List<SysAttachment>> getAttachmentByName(@RequestBody @Valid GetAttachmentRequest getAttachmentRequest) {
        return ResultBean.restResult(sysAttachmentService.getAttachmentByName(getAttachmentRequest), ErrorCodeInfo.OK);
    }

    @PostMapping("/getAttachmentByViews")
    @Operation(summary = "热度查找")
    public ResultBean<List<SysAttachment>> getAttachmentByViews(@RequestBody @Valid String type) {
        return ResultBean.restResult(sysAttachmentService.getAttachmentListByViews(type), ErrorCodeInfo.OK);
    }

    @PostMapping("/getAttachmentByTime")
    @Operation(summary = "时间查找")
    public ResultBean<List<SysAttachment>> getAttachmentByTime(@RequestBody @Valid String type) {
        return ResultBean.restResult(sysAttachmentService.getAttachmentListByTime(type), ErrorCodeInfo.OK);
    }

    @PostMapping("/saveTournamentList")
    @Operation(summary = "存入赛事回放/赛事图片等")
    public ResultBean<Boolean> saveTournamentList(@RequestBody @Valid InsertTournamentRequest insertTournamentRequest) {
        return ResultBean.restResult(sysAttachmentService.saveTournamentList(insertTournamentRequest), ErrorCodeInfo.OK);
    }

    @PostMapping("/uploadVideo")
    @Operation(summary = "上传视频")
    public ResultBean<List<SysAttachment>> uploadVideo(@RequestParam(value="file") List<MultipartFile> fileList) {
        return ResultBean.restResult(sysAttachmentService.uploadVideo(fileList), ErrorCodeInfo.OK);
    }

    @PostMapping("/uploadPhotos")
    @Operation(summary = "上传图片")
    public ResultBean<List<SysAttachment>> uploadPhotos(@RequestParam(value="file") List<MultipartFile> fileList) {
        return ResultBean.restResult(sysAttachmentService.uploadPhotos(fileList), ErrorCodeInfo.OK);
    }

    @PostMapping("/uploadNews")
    @Operation(summary = "上传新闻")
    public ResultBean<List<SysAttachment>> uploadNews(@RequestParam(value="file") List<MultipartFile> fileList) {
        return ResultBean.restResult(sysAttachmentService.uploadNews(fileList), ErrorCodeInfo.OK);
    }

    @PostMapping("/saveAttachmentList")
    @Operation(summary = "存入多个附件")
    public ResultBean<Boolean> saveAttachmentList(@RequestBody @Valid InsertAttachmentRequest insertAttachmentRequest) {
        return ResultBean.restResult(sysAttachmentService.saveAttachmentList(insertAttachmentRequest), ErrorCodeInfo.OK);
    }

    @PostMapping("/insertViews")
    @Operation(summary = "点击量增加")
    public ResultBean<Boolean> insertViews(@RequestBody @Valid String attachmentId) {
        return ResultBean.restResult(fileUploadService.insertViews(attachmentId), ErrorCodeInfo.OK);
    }

    @PostMapping("/test")
    @Operation(summary = "测试")
    public ResultBean<Boolean> insertViews() {
        return ResultBean.restResult(Boolean.TRUE, ErrorCodeInfo.OK);
    }
}
